//
//  TTDatabaseRow.m
//  Tint
//
//  Created by Kevin Wu on 2/28/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTDatabaseRow.h"
#import <objc/runtime.h>
#import "TTDatabaseResult.h"



@implementation TTDatabaseRow


#pragma mark - Accessors

@synthesize columnData = _columnData;
@synthesize databaseResult = _databaseResult;



#pragma mark - Memory

- (id)initWithDatabaseResult:(TTDatabaseResult *)result
{
    self = [super init];
    if ( self ) {
        _columnData = [[NSMutableArray alloc] init];
        _databaseResult = result;
    }
    return self;
}

- (void)dealloc
{
    TTRelease(_columnData);
    _databaseResult = nil;
    [super dealloc];
}


#pragma mark - Public

- (NSUInteger)columnIndexForName:(NSString *)columnName
{
	return [_databaseResult.columnNames indexOfObject:columnName];
}



- (BOOL)boolForColumn:(NSString *)columnName
{
    return [self boolForColumnAtIndex:[self columnIndexForName:columnName]];
}

- (BOOL)boolForColumnAtIndex:(NSUInteger)columnIndex
{
    return ( [self intForColumnAtIndex:columnIndex] != 0 );
}


- (int)intForColumn:(NSString *)columnName
{
    return [self intForColumnAtIndex:[self columnIndexForName:columnName]];
}

- (int)intForColumnAtIndex:(NSUInteger)columnIndex
{
    return [[_columnData objectOrNilAtIndex:columnIndex] intValue];
}


- (long long)longLongForColumn:(NSString *)columnName
{
    return [self longLongForColumnAtIndex:[self columnIndexForName:columnName]];
}

- (long long)longLongForColumnAtIndex:(NSUInteger)columnIndex
{
    return [[_columnData objectOrNilAtIndex:columnIndex] longLongValue];
}


- (double)doubleForColumn:(NSString *)columnName
{
    return [self doubleForColumnAtIndex:[self columnIndexForName:columnName]];
}

- (double)doubleForColumnAtIndex:(NSUInteger)columnIndex
{
    return [[_columnData objectOrNilAtIndex:columnIndex] doubleValue];
}


- (NSString *)stringForColumn:(NSString *)columnName
{
    return [self stringForColumnAtIndex:[self columnIndexForName:columnName]];
}

- (NSString *)stringForColumnAtIndex:(NSUInteger)columnIndex
{
    return [_columnData objectOrNilAtIndex:columnIndex];
}


- (NSDate *)dateForColumn:(NSString *)columnName
{
    return [self dateForColumnIndex:[self columnIndexForName:columnName]];
}

- (NSDate *)dateForColumnIndex:(NSUInteger)columnIndex
{
    return TTDateFromRFC3339DateTimeString([_columnData objectOrNilAtIndex:columnIndex]);
}

@end




@implementation NSObject (DatabaseRow)

- (void)loadValueFromDatabaseRow:(TTDatabaseRow *)row
{
    if ( row == nil ) {
        return;
    }
    
    NSArray *propertyNames = [[self class] newPropertyNames];
    NSDictionary *propertyAttributes = [[self class] newPropertyAttributes];
    
    for ( int i=0; i<[propertyNames count]; ++i ) {
        NSString *name = [propertyNames objectAtIndex:i];
        NSString *attribute = [propertyAttributes objectForKey:name];
        
        if ( [row.databaseResult.columnNames hasObjectEqualTo:name] ) {
            if ( [attribute hasPrefix:@"Tc"] ) { // BOOL: Tc
                id value = [[NSNumber alloc] initWithBool:[row boolForColumn:name]];
                [self setValue:value forKey:name];
                TTRelease(value);
            } else if ( [attribute hasPrefix:@"Ti"] ) { // int: Ti
                id value = [[NSNumber alloc] initWithInt:[row intForColumn:name]];
                [self setValue:value forKey:name];
                TTRelease(value);
            } else if ( [attribute hasPrefix:@"Tq"] ) { // long long: Tq
                id value = [[NSNumber alloc] initWithLongLong:[row longLongForColumn:name]];
                [self setValue:value forKey:name];
                TTRelease(value);
            } else if ( [attribute hasPrefix:@"Td"] ) { // double: Td
                id value = [[NSNumber alloc] initWithDouble:[row doubleForColumn:name]];
                [self setValue:value forKey:name];
                TTRelease(value);
            } else if ( [attribute hasPrefix:@"T@\"NSString\""] ) { // NSString: T@"NSString"
                id value = [row stringForColumn:name];
                [self setValue:value forKey:name];
            } else if ( [attribute hasPrefix:@"T@\"NSDate\""] ) { // NSDate: T@"NSDate"
                id value = [row dateForColumn:name];
                [self setValue:value forKey:name];
            }
        }
        
    }
    
    TTRelease(propertyNames);
    TTRelease(propertyAttributes);
}

@end
